<template>
    <div class="wiper">
        <BizFilter title="AGV主数据" :items="filterItems" :model="filterModel" />
        <BizTable :buttons="buttons" :columns="columns" :data="data" :pagination="filterModel" />
        <Form ref="formRef" v-model:show="show" :rules="rules" :model="formData" :items="formItems"/>
    </div>
</template>
<script>
import BizFilter from "../../components/Filter";
import BizTable from "../../components/Table";
import {
    ColumnProps, FilterItems, FilterModel,
    FormProps,FormItems, RuleProps
} from "./config";
import { pager,save,details,remove } from "@/api/car";
import { Dictionary } from "../../utils/Dictionary";
import Form from "@/components/Form/index.vue";
export default {
    name: 'car',
    data() {
        return {
            columns: ColumnProps,
            formData: FormProps,
            formItems: FormItems,
            rules: RuleProps,
            filterItems: FilterItems,
            filterModel: FilterModel,
            data: [{}],
            buttons: [],
            show: false,
        }
    },
    components: {
      Form,
        BizFilter,
        BizTable
    },
    computed: {
        isDictionary() {
            return this.$store.state.dictionary
        }
    },
    watch: {
        isDictionary(data) {
            this._carType(data);
        }
    },
    mounted() {
        this.load();
        this._carType(this.$store.state.dictionary);
        this._zone(this.$store.state.dictionary);
    },
    methods: {
        load() {
            pager(this.filterModel).then(res => {
                this.data = res.data.rows;
                this.filterModel.total = res.data.total;
            })
        },

        /**
         * 添加方法
         */
        add() {
          this.formData.id = "";
          this.$refs.formRef.resetField();
          this.show = true;
        },

        /**
         * 修改方法
         */
        modify(ids) {
          this.show = true;
          details(ids).then(res => {
            this.formData = res.data;
          })
        },

        /**
         * 提交
         */
        submit() {
          return new Promise((resolve, reject) => {
            console.log("提交fromdata",this.formData);
            this.formData.locationLock = this.formData.locationLock === "1";
            save(this.formData).then(res => {
              this.load();
              return resolve(res);
            }).catch((e) => {
              return reject(e);
            })
          })
        },

        /**
         * 删除方法
         */
        delete(ids) {
          return new Promise((resolve, reject) => {
            remove(ids.join(",")).then(res => {
              this.load();
              return resolve(res);
            }).catch(e => {
              return reject(e);
            })
          })
        },
        /**
         * 重命名
         * @private
         */
        _rename(list, key = "id") {
            return list?.map(item => {
                return { label: item.name, value: item[key], children: this._rename(item.children) }
            });
        },
        /**
         * 查找托盘类型
         * @private
         */
        _carType(data) {
            const carTypes = this._rename(data.filter(item => item.code === Dictionary.CAR_TYPE)
                .map(item => item.children)[0], "code");
            this.filterItems.filter(item => item.key === 'agvType')[0].data = carTypes;
            this.formItems.filter(item => item.key === 'agvType')[0].data = carTypes;
        },

        _zone(data){
          const zones = this._rename(data.filter(item => item.code === Dictionary.LOCATION_ZONE)
            .map(item => item.children)[0], "code");
          this.formItems.filter(item => item.key === 'zone')[0].data = zones;
        }

    }
}
</script>
<style></style>
